<!DOCTYPE html>
<html>
<head>
	{include file='top.tpl'}
</head>
<body class="skin-blue">
<div class="wrapper">
	{include file="bread_crumbs.tpl"}
	<!--引入面包屑-->
	<section class="web-setting-wrap">
		<div class="row">
			<div class="col-md-12">
				<div class="box">
	                <div class="box-header with-border clearfix">
	                	<p class="box-title-sm pull-left no-margin">
							<span>用户名：{$user_name}</span>
							<span>姓名：{$name}</span>
	                	</p>
	                </div><!-- /.box-header -->
	                <form>
	                <div class="box-body">	                	
	                    <table class="table table-bordered">
                    		<tr>
                    			<td height="25" width="150">
                    				<span>授权角色</span>
                    				<span class="checkbox no-margin"><label><input type="checkbox" name="is_show" id="js_checkAllRole">全选</label></span>
                    			</td>
                    			<td>
                    				<table class="table no-margin auth-role">
                    					<tr>
                    						<td class="no-padding no-border clearfix" id="js_roleCategory">
                    						{if count($allRole)>0}
                   							{foreach from=$allRole item=role}
                    							<span class="checkbox"><label><input type="checkbox" name="role_type[]" {if isset($newUserRole[$role.role_id]) && $newUserRole[$role.role_id] eq 1}checked="checked"{/if} value='{$role.role_id}'>{$role.role_name}</label></span>
                    						{/foreach}
                    						{/if}
                    						</td>
                    					</tr>
                    				</table>
                    			</td>
                    		</tr>
	                    		
                    		<tr>
                    			<td height="25" width="150">
                    				<span>授权分类</span>
                    				<span class="checkbox no-margin"><label><input type="checkbox" name="is_show" id="js_checkAllCate">全选</label></span>
                    			</td>
                    			<td>
                    				<table class="table no-margin auth-warehouse">
                    					<tr>
                    						<td class="no-border">
												<div class="auth-warehouse-list text-left ztree" id="js_authCategory">
													
												</div>
                    						</td>	                    						
                    					</tr>
                    				</table>
                    			</td>
                    		</tr>
	                    		
                    		<tr>
                    			<td height="25" width="150">
                    				<span>授权仓库</span>
                    				<span class="checkbox no-margin"><label><input type="checkbox" name="is_show" id="js_checkAllWare">全选</label></span>
                    			</td>
                    			<td>
                    				<table class="table no-margin auth-warehouse">
                    					<tr>
                    						<td class="no-border">
												<div class="auth-warehouse-list text-left ztree" id="js_authWarehouse">
													
												</div>
                    						</td>	                    						
                    					</tr>
                    				</table>
                    			</td>
                    		</tr>
	                    </table>
	                </div>
	                <!-- .box-body -->
	                <div class="box-footer clearfix text-center">
	                	<input type="hidden" name="checked_auth_category" id="js_checkedAuthCategory" value='{$userCatesInfo_new}'>
	                	<input type="hidden" name="checked_auth_warehouse" id="js_checkedAuthWarehouse" value='{$userWareInfo_new}'>
	                	<input type="hidden" name="updateId" id="updateId" value='{$user_id}'>
                 		<button type="button" class="btn btn-primary btn-sm" id='save_auth'>确定</button>
                 		<button type="button" class="btn btn-warning btn-sm" id="js_resetForm">重置</button>
                 		<a id="back" class="btn btn-default btn-sm" href="javascript:;" onclick="history.back();">返回</a>
	                </div>
	                </form>
	            </div>
			</div>
		</div>
	</section>
</div>
{include file='foot.tpl'}	
{literal}
<script>
(function(){
	var setting = {
		view: {
			selectedMulti: false,
			showIcon: false
		},
		check: {
			enable: true
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback: {
			onCheck:onCheck
		}
	};
	var wsetting = {
		view: {
			selectedMulti: false,
			showIcon: false
		},
		check: {
			enable: true
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback: {
			onCheck:onCheckWare
		}
	};
	{/literal}
        var zNodes = {$cate_json}; 
        var wNodes = {$ware_json};
    {literal}
	$.fn.zTree.init($("#js_authCategory"), setting, zNodes);
	$.fn.zTree.init($("#js_authWarehouse"), wsetting, wNodes);

	var zTreeObj = $.fn.zTree.getZTreeObj("js_authCategory");

	function onCheck(e, treeId, treeNode) {
		var tempArr = [],
		checkCount = zTreeObj.getCheckedNodes().length, //获取选中
		nocheckCount = zTreeObj.getCheckedNodes(false).length; //没有选中

		if(checkCount == checkCount + nocheckCount){
			$("#js_checkAllCate").prop("checked",true);
		}
		else{
			$("#js_checkAllCate").prop("checked",false);
		}

        for(var i=0;i<checkCount;i++){
        	tempArr.push(zTreeObj.getCheckedNodes()[i].id);        	
        }
        $("#js_checkedAuthCategory").val(tempArr.join(","));
	}

	var zTreeObjWare = $.fn.zTree.getZTreeObj("js_authWarehouse");
	function onCheckWare(e, treeId, treeNode) {
		var tempArr = [],
		checkCount = zTreeObjWare.getCheckedNodes().length, //获取选中
		nocheckCount = zTreeObjWare.getCheckedNodes(false).length; //没有选中

		if(checkCount == checkCount + nocheckCount){
			$("#js_checkAllCate").prop("checked",true);
		}
		else{
			$("#js_checkAllCate").prop("checked",false);
		}

        for(var i=0;i<checkCount;i++){
        	tempArr.push(zTreeObjWare.getCheckedNodes()[i].id);        	
        }
        $("#js_checkedAuthWarehouse").val(tempArr.join(","));
	}

	//全选反选ztree
	$("#js_checkAllCate").on("change",function(){		
		var $this=$(this);
		if($this.prop("checked")){
			zTreeObj.checkAllNodes(true);
		}
		else{
			zTreeObj.checkAllNodes(false);
		}
		onCheck();
	});
	$("#js_checkAllWare").on("change",function(){		
		var $this=$(this);
		if($this.prop("checked")){
			zTreeObjWare.checkAllNodes(true);
		}
		else{
			zTreeObjWare.checkAllNodes(false);
		}
		onCheckWare();
	});
})();

//授权角色全选
(function(){
	$("input[name='role_type[]']").on("change",function(){
		var _len = $("input[name='role_type[]']").length,
			_checked = $("input[name='role_type[]']").filter(":checked").length;
		if(_len==_checked){
			$("#js_checkAllRole").prop("checked",true);
		}
		else{
			$("#js_checkAllRole").prop("checked",false);
		}
	});			
	$("#js_checkAllRole").on("change",function(){
		var $this = $(this);
		if($this.prop("checked")==true){
			$("#js_roleCategory").find("input[type='checkbox']").prop("checked",true);
		}
		else{
			$("#js_roleCategory").find("input[type='checkbox']").prop("checked",false);
		}
	});
})();

$("#save_auth").click(function(){
	  updateId = $('#updateId').val();
	  roles = new Array();
	  $('input[name="role_type[]"]:checked').each(function(){
		  roles.push($(this).val());
	  });

	  cates = $('#js_checkedAuthCategory').val();
	  wares = $("#js_checkedAuthWarehouse").val();

	  if(updateId>0)
	  {
		  $.ajax({
	          type:"POST",
	          url:"/system/user/setuserrole",
	          data:{'roles':roles,'wares':wares,'cates':cates,'user_id':updateId},
	          dataType: "json",
	          success:function(res){
	          	  if (res.status == 1) {
	          	  	layer.alert(res.content);
	              	location.href='/system/user/setprivilege?id='+updateId;
	          	  }else{
	          	  	layer.alert(res.message);
	          	  }
	          }
	      });
	   }else{
		   layer.alert('异常'); 
	   }
});

//重置按钮
$("#js_resetForm").on("click",function(){
	if(confirm("确认重置吗？")){
		$.fn.zTree.getZTreeObj("js_authCategory").checkAllNodes(false);
		$(".box-body").find("input:checkbox").prop("checked",false);
	}
});	
</script>
{/literal}
</body>
</html>
